package com.geek.controller;

import com.geek.bean.BaseRespVo;
import com.geek.model.*;
import com.geek.service.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RequestMapping("wx/home")
@RestController
public class WxHomeController {

    @Autowired
    AdService adService;
    @Autowired
    CategoryService categoryService;
    @Autowired
    SystemService systemService;
    @Autowired
    BrandService brandService;
    @Autowired
    CouponService couponService;
    @Autowired
    GoodsService goodsService;
    @Autowired
    TopicService topicService;

    @GetMapping("index")
    public BaseRespVo index() {
        Map<String, Object> dataMap = new HashMap<>();
        //首页轮播图
        List<MarketAd> banner = adService.query();
        dataMap.put("banner", banner);

        // 类目信息
        List channels = categoryService.queryLevelOne();
        dataMap.put("channel", channels);

        // 获得system表中的数据
        Map<String, String> systemConfig = systemService.toMap();

        //品牌制造商直供
        int brandLimit = Integer.parseInt(systemConfig.get("market_wx_index_brand"));
        List<MarketBrand> brandList = brandService.list(2, brandLimit);
        dataMap.put("brandList", brandList);

        // 优惠券 没有登录就查询所有可用优惠券的前三个
        // 有登录就查询用户可用的优惠券
        List<MarketCoupon> couponList = couponService.list(1,3);
        dataMap.put("couponList", couponList);

        //floorGoodsList
        int categoryLimit = Integer.parseInt(systemConfig.get("market_wx_catlog_list"));
        int categoryGoodsLimit = Integer.parseInt(systemConfig.get("market_wx_catlog_goods"));
        List floorGoodsList = goodsService.floorGoodsList(categoryLimit, categoryGoodsLimit);
        dataMap.put("floorGoodsList", floorGoodsList);

        //新品首发 和 人气推荐
        int hotLimit = Integer.parseInt(systemConfig.get("market_wx_index_hot"));
        int newLimit = Integer.parseInt(systemConfig.get("market_wx_index_new"));
        List<MarketGoods> hotGoodsList = goodsService.goodsList(true,false,1,hotLimit);
        List<MarketGoods> newGoodsList = goodsService.goodsList(false,true,1,newLimit);
        dataMap.put("hotGoodsList", hotGoodsList);
        dataMap.put("newGoodsList", newGoodsList);

        //专题精选
        int topicLimit = Integer.parseInt(systemConfig.get("market_wx_index_topic"));
        List<MarketTopic> topicList = topicService.topicList(1,topicLimit);
        dataMap.put("topicList", topicList);
        return BaseRespVo.ok(dataMap);

    }
}
